package com.mzpai.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gexin.fastjson.JSONObject;
import com.mzpai.board.model.OperateLog;
import com.mzpai.board.service.OperateCashLogService;
import com.mzpai.board.service.OperateFlowLogService;
import com.mzpai.board.service.OperateLogService;
import com.mzpai.board.vo.OperateLogReqVo;
import com.mzpai.util.Constants;
import com.mzpai.util.DateUtil;

@RestController
@RequestMapping("/interface")
public class InterfaceController extends BaseController {
	
	protected Logger logger = LoggerFactory.getLogger(InterfaceController.class);
	
	@Autowired
	private OperateCashLogService operateCashLogService;
	@Autowired
	private OperateFlowLogService operateFlowLogService;
	@Autowired
	private OperateLogService operateLogService;
	
	
	/**
	 * 流量数据统计  
	 * 定时任务 每整点执行
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/tjFlowInfo")
	public void tjFlowInfo(HttpServletRequest request, HttpServletResponse response) throws Exception { 
		JSONObject o = new JSONObject();
		try{
			logger.info("msg1=定时执行流量统计时间={}", DateUtil.getNow());
			operateFlowLogService.saveFlowInfo(request.getParameter("time"));
			o.put(Constants.Result.SUCCESS, "操作成功！");
		} catch(Exception e) {
			o.put(Constants.Result.FAILURE, "type类型为空！");
			logger.error("执行流量统计时异常, e={}",  e);
		}
		super.printWriter(response, o.toString());
	}
	
	/**
	 * 现金流数据统计  
	 * 定时任务 每整点执行
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/tjCashInfo")
	public void tjCashInfo(HttpServletRequest request, HttpServletResponse response) throws Exception { 
		JSONObject o = new JSONObject();
		try{
			logger.info("msg1=定时执行现金统计时间={}", DateUtil.getNow());
			operateCashLogService.saveCashInfo(request.getParameter("time"));
			o.put(Constants.Result.SUCCESS, "操作成功！");
		} catch(Exception e) {
			o.put(Constants.Result.FAILURE, "type类型为空！");
			logger.error("执行现金流统计时异常, e={}",  e);
		}
		super.printWriter(response, o.toString());
	}
	
	
	
	/**
	 * 根据采集数据收集访问过商家首页且没绑定店铺数据自动绑定 
	 * 定时任务10分钟执行一次
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/automaticbindStore")
	public void automaticbindStore(HttpServletRequest request, HttpServletResponse response) throws Exception { 
		JSONObject o = new JSONObject();
		try{
			logger.info("msg1=根据采集数据收集访问过商家首页且没绑定店铺数据自动绑定开始");
			OperateLogReqVo reqVo=new OperateLogReqVo();
			// type：1=天数 ，2=小时数 3=月数 4分钟数 5秒数
			Date beforeTwoHour=DateUtil.getNewDate(new Date(), -10,4);
			String beginDateStr=DateUtil.date2Str(beforeTwoHour,DateUtil.DATE_HH_MM_SS);
			String endDateStr=DateUtil.getNow();
			reqVo.setBeginDate(beginDateStr);
			reqVo.setEndDate(endDateStr);
			reqVo.setKindCode("2");
			List<String> storeIdNotInList=new ArrayList<String>();
			storeIdNotInList.add("0");
			reqVo.setStoreIdNotInList(storeIdNotInList);
			List<OperateLog> list=operateLogService.getFirstStoreDetailList(reqVo);
			//判断是否绑定无则绑定店铺
			
			o.put(Constants.Result.SUCCESS, "操作成功！");
		} catch(Exception e) {
			o.put(Constants.Result.FAILURE, "type类型为空！");
			logger.error("执行根据采集数据收集访问过商家首页且没绑定店铺数据自动绑定时异常, e={}",  e);
		}
		super.printWriter(response, o.toString());
	}

}
